package com.example.hemin.fnb.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hemin.fnb.R;
import com.example.hemin.fnb.ui.adapter.ShopAddressMessageAdapter;
import com.example.hemin.fnb.ui.base.BaseMvpActivity;
import com.example.hemin.fnb.ui.bean.ShopMessageBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopAddress extends BaseMvpActivity {
    @BindView(R.id.lay_back)
    LinearLayout layBack;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.titl2)
    TextView titl2;
    @BindView(R.id.user_toolbar)
    LinearLayout userToolbar;
    @BindView(R.id.shop_address_recyclerview)
    RecyclerView shopAddressRecyclerview;
    private List<ShopMessageBean> beans = new ArrayList<>();
    @Override
    public int getLayoutId() {
        return R.layout.shop_address;
    }

    @Override
    public void initView() {
        title.setText("我的收货地址");
        titl2.setVisibility(View.VISIBLE);
        SharedPreferences sp = getSharedPreferences("userAddress",MODE_PRIVATE);
        String name = sp.getString("name","");
        String phone = sp.getString("phone","");
        String address = sp.getString("address","");
        int status = sp.getInt("status",0);

        if(name != "" && phone != "" && address != ""){
            ShopMessageBean bean = new ShopMessageBean(name, phone, address, status);
            beans.add(bean);
            initData();
        }



    }
    private void initData(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        shopAddressRecyclerview.setLayoutManager(linearLayoutManager);
        final ShopAddressMessageAdapter adapter = new ShopAddressMessageAdapter(R.layout.shop_address_message_adapter,beans);
        shopAddressRecyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapte, View view, int position) {
                Intent intent = new Intent(ShopAddress.this, ShopAddressMessage.class);
                intent.putExtra("name",adapter.getData().get(position).getName());
                intent.putExtra("phone",adapter.getData().get(position).getPhone());
                intent.putExtra("address",adapter.getData().get(position).getAddress());
                intent.putExtra("status",adapter.getData().get(position).getStatus());
                intent.putExtra("isStatus",1);
                startActivityForResult(intent,666);
            }
        });
    }



    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.lay_back, R.id.titl2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lay_back:
                finish();
                break;
            case R.id.titl2:
                Intent intent = new Intent(ShopAddress.this, ShopAddressMessage.class);
                intent.putExtra("isStatus",1);
                startActivityForResult(intent,666);
                break;


        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 1 ){
            SharedPreferences sp = getSharedPreferences("userAddress",MODE_PRIVATE);
            String name = sp.getString("name","");
            String phone = sp.getString("phone","");
            String address = sp.getString("address","");
            int status = sp.getInt("status",0);
            ShopMessageBean bean = new ShopMessageBean(name,phone,address,status);
            beans.add(bean);
            initData();
        }
    }
}
